//
//  XWXSqliteQueryCommand+Query.m
//  XWXSqlite
//
//  Created by 许卫星 on 17/9/5.
//  Copyright © 2017年 wxxu. All rights reserved.
//

#import "XWXSqliteQueryCommand+Query.h"
#import "NSString+XWXSqliteSQL.h"
#import "XWXSqliteConfiguration.h"
#import "XWXSqliteDataBase.h"

@implementation XWXSqliteQueryCommand (Query)

- (XWXSqliteQueryCommand *)select:(NSString *)columList isDistinct:(BOOL)isDistinct
{
    [self resetQueryCommand];
    
    if (columList == nil) {//DISTINCT去重
        if (isDistinct) {
            [self.sqlString appendString:@"SELECT DISTINCT * "];
        } else {
            [self.sqlString appendString:@"SELECT * "];
        }
    } else {
        if (isDistinct) {
            [self.sqlString appendFormat:@"SELECT DISTINCT %@ ", [columList safeSQLEncode]];
        } else {
            [self.sqlString appendFormat:@"SELECT %@ ", [columList safeSQLEncode]];
        }
    }
    
    return self;
}

- (XWXSqliteQueryCommand *)from:(NSString *)fromList
{
    if (fromList == nil) {
        return self;
    }
    [self.sqlString appendFormat:@"FROM %@ ", [fromList safeSQLEncode]];
    return self;
}

- (XWXSqliteQueryCommand *)where:(NSString *)condition
{
    if (condition == nil) {
        return self;
    }
    
    NSString *whereString = [condition safeSQLEncode];
    [self.sqlString appendFormat:@"WHERE %@ ", whereString];
    
    return self;
}

- (XWXSqliteQueryCommand *)whereColumnName:(NSString *)name relation:(ColumnNameToValueRelationType)relationType value:(id)value valueType:(ColumnValueRelationType)valueType
{
    if (name == nil || value == nil) {
        return self;
    }
    
    NSString *relation = [self columnNameToValueRelationTypeDic][@(relationType)];
    NSString *whereString;
    if (valueType == ColumnValueRelationDataType) {
        whereString = [NSString stringWithFormat:@"%@ %@ %@",name,relation,value];
    }else{
        whereString = [NSString stringWithFormat:@"%@ %@ '%@'",name,relation,value];
    }
    [self.sqlString appendFormat:@"WHERE %@ ", [whereString safeSQLEncode]];
    
    return self;
}

//ORDER BY 语句用于根据指定的列对结果集进行排序。
- (XWXSqliteQueryCommand *)orderBy:(NSString *)orderBy isDESC:(BOOL)isDESC
{
    if (orderBy == nil) {
        return self;
    }
    [self.sqlString appendFormat:@"ORDER BY %@ ", [orderBy safeSQLMetaString]];
    if (isDESC) {
        [self.sqlString appendString:@"DESC "];
    } else {
        [self.sqlString appendString:@"ASC "];
    }
    return self;
}

//取几条数据
- (XWXSqliteQueryCommand *)limit:(NSInteger)limit
{
    if (limit == XWXSqliteNoLimit) {
        return self;
    }
    [self.sqlString appendFormat:@"LIMIT %lu ", (unsigned long)limit];
    return self;
}

//从第几条开始取
- (XWXSqliteQueryCommand *)offset:(NSInteger)offset
{
    if (offset == XWXSqliteNoOffset) {
        return self;
    }
    [self.sqlString appendFormat:@"OFFSET %lu ", (unsigned long)offset];
    return self;
}

//从offset开始取limit条纪录
- (XWXSqliteQueryCommand *)limit:(NSInteger)limit offset:(NSInteger)offset
{
    return [[self limit:limit] offset:offset];
}

//表中的纪录数
- (XWXSqliteQueryCommand *)countAll
{
    [self.sqlString appendFormat:@"SELECT COUNT(*) "];
    return self;
}

//拿到最新插入行的记录ID
- (NSNumber *)lastInsertRowId
{
    return @(sqlite3_last_insert_rowid(self.database.database));
}

//最近执行的INSERT、UPDATE和DELETE语句所影响的数据行数
- (NSNumber *)rowsChanged
{
    return @(sqlite3_changes(self.database.database));
}

- (NSDictionary *)columnNameToValueRelationTypeDic
{
    return @{
             @(ColumnNameToValueRelationTypeMore):@" > ",
             @(ColumnNameToValueRelationTypeLess):@" < ",
             @(ColumnNameToValueRelationTypeEqual):@" = ",
             @(ColumnNameToValueRelationTypeMoreEqual):@" >= ",
             @(ColumnNameToValueRelationTypeLessEqual):@" <= "
             };
}

- (NSDictionary *)columnNameToValueLogicTypeDic
{
    return @{
             @(ColumnNameToValueLogicTypeNot): @" NOT ",
             @(ColumnNameToValueLogicTypeAnd):@" AND ",
             @(ColumnNameToValueLogicTypeOr):@" OR "
             };
}
@end
